package com.cornergj.back.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cornergj.model.Admin;
import com.cornergj.model.ChinaDataStatistice;
import com.cornergj.model.Entity;
import com.cornergj.model.Entity.AdminCriteria;
import com.cornergj.model.Entity.FrontImgCriteria;
import com.cornergj.model.Entity.OrderServiceCriteria;
import com.cornergj.model.Entity.Pagination;
import com.cornergj.model.Entity.ServiceContentCriteria;
import com.cornergj.model.Entity.Value;
import com.cornergj.model.FrontImg;
import com.cornergj.model.Nanny;
import com.cornergj.model.OrderService;
import com.cornergj.model.ServiceContent;
import com.cornergj.model.SiteOfProvince;
import com.cornergj.response.ErrorCode;
import com.cornergj.response.JsonResult;
import com.cornergj.service.AdminService;
import com.cornergj.service.FrontImgService;
import com.cornergj.service.NannyService;
import com.cornergj.service.OrderServiceService;
import com.cornergj.service.ServiceContentService;
import com.cornergj.service.SiteOfProvinceService;
import com.cornergj.uitl.FileUtils;


/**
 * Title:
 * Model:
 * Description:
 * @author 郑耿杰
 * @date 2018年2月2日
 */
@Controller
@RequestMapping("/statistics")
public class StatisticsController {
	
	@Autowired
	private ServiceContentService serviceContentService;
	@Autowired
	private OrderServiceService orderServiceService;
	@Autowired
	private NannyService nannyService;
	@Autowired
	private SiteOfProvinceService siteOfProvinceService;
	/**
	 * 分页方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/mainstatistics", method = RequestMethod.GET)
	public String mainstatistics(HttpServletRequest request) {
		List<SiteOfProvince> provincelist=siteOfProvinceService.selectAll();
		List<ChinaDataStatistice> statisticelist=new ArrayList<ChinaDataStatistice>();
		List<Integer> statisticebymonth=new ArrayList<Integer>();
		List<Integer> statisticebyweek=new ArrayList<Integer>();
		Map<Integer,Integer> monthsraris=new HashMap();
		Map<Integer,Integer> weeksraris=new HashMap();
		monthsraris.put(1, 0);
		monthsraris.put(2, 0);
		monthsraris.put(3, 0);
		monthsraris.put(4, 0);
		monthsraris.put(5, 0);
		monthsraris.put(6, 0);
		monthsraris.put(7, 0);
		monthsraris.put(8, 0);
		monthsraris.put(9, 0);
		monthsraris.put(10, 0);
		monthsraris.put(11, 0);
		monthsraris.put(12, 0);
		
		weeksraris.put(1, 0);
		weeksraris.put(2, 0);
		weeksraris.put(3, 0);
		weeksraris.put(4, 0);
		weeksraris.put(5, 0);
		weeksraris.put(6, 0);
		weeksraris.put(7, 0);
		
		for(SiteOfProvince province:provincelist){
			ChinaDataStatistice dataStatistice=new ChinaDataStatistice();
			String provicename=province.getProvince();
			if(provicename.endsWith("市")){
				provicename=provicename.substring(0, provicename.length()-1);
			}else if(provicename.endsWith("省")){
				provicename=provicename.substring(0, provicename.length()-1);
			}else if(provicename.endsWith("自治区")){
				provicename=provicename.substring(0, provicename.length()-3);
			}else if(provicename.endsWith("回族自治区")){
				provicename=provicename.substring(0, provicename.length()-5);
			}else if(provicename.endsWith("维吾尔自治区")){
				provicename=provicename.substring(0, provicename.length()-6);
			}else if(provicename.endsWith("特别行政区")){
				provicename=provicename.substring(0, provicename.length()-5);
			}
			
			dataStatistice.setProvicename(provicename);
			OrderServiceCriteria criteria=new OrderServiceCriteria();
			criteria.setServiceProvince(Value.eq(province.getId()));
			dataStatistice.setOrdernum(orderServiceService.selectList(criteria).size());
			statisticelist.add(dataStatistice);
		}
		
		List<OrderService> orderlist=orderServiceService.selectAll();
		for(OrderService order:orderlist){
			int cmonth=order.getCreateTime().getMonth()+1;
			int mordernum=monthsraris.get(cmonth);
			monthsraris.put(cmonth, mordernum+1);
		}
		
		Calendar nowcal=Calendar.getInstance(Locale.CHINA);
		nowcal.set(Calendar.HOUR_OF_DAY, 0);
		nowcal.set(Calendar.MINUTE, 0);
		nowcal.set(Calendar.SECOND, 0);
		nowcal.setFirstDayOfWeek(Calendar.MONDAY);
		//周一
		nowcal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		Date mondaydate=nowcal.getTime();
		nowcal.set(Calendar.HOUR_OF_DAY, 23);
		nowcal.set(Calendar.MINUTE, 59);
		nowcal.set(Calendar.SECOND, 59);
		//周日
		nowcal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		Date sundaydate=nowcal.getTime();
		
		for(OrderService order:orderlist){
			Date date=order.getCreateTime();
			if(date.after(mondaydate)&&date.before(sundaydate)){
				Calendar cal = Calendar.getInstance();
				cal.setTime(date);
				//判断周几，周日为0，周六为6
				int weekday=cal.get(Calendar.DAY_OF_WEEK)-1;
				if (weekday < 0){
					weekday = 0;
				}
				if(weekday==0){
					int getval=weeksraris.get(7);
					weeksraris.put(7, getval+1);
				}else{
					int getval=weeksraris.get(weekday);
					weeksraris.put(weekday, getval+1);
				}
				
			}
			
		}
		
		for(Map.Entry<Integer,Integer> entry:monthsraris.entrySet()){
			statisticebymonth.add(entry.getValue());
		}
		for(Map.Entry<Integer,Integer> entry:weeksraris.entrySet()){
			statisticebyweek.add(entry.getValue());
		}
		request.setAttribute("statisticebyweek", statisticebyweek);
		request.setAttribute("statisticebymonth", statisticebymonth);
		request.setAttribute("provincelist", statisticelist);
		return "statistics/main";
	}
}
